package com.example.sjkks.controller;

import com.example.sjkks.entity.Teacher;
import com.example.sjkks.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:8081")
public class TeacherController {

    @Autowired
    private TeacherMapper teacherMapper;

    @Transactional
    @GetMapping("/teachers")
    public ResponseEntity<List<Teacher>> getAllTeachers() {
        try {
            List<Teacher> teachers = teacherMapper.findAll();
            return ResponseEntity.ok(teachers);
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @Transactional
    @PostMapping("/register")
    public ResponseEntity<Object> registerTeacher(@RequestBody Teacher teacher) {
        try {
            System.out.println("Received register request for ID: " + teacher.getId()); // 添加日志
            Teacher existingTeacher = teacherMapper.findById(teacher.getId());
            if (existingTeacher != null) {
                return ResponseEntity.badRequest().body("用户名已存在");
            }
            teacherMapper.save(teacher);
            return ResponseEntity.ok("注册成功");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @Transactional
    @PostMapping("/login")
    public ResponseEntity<Object> loginTeacher(@RequestBody Teacher teacher) {
        try {
            System.out.println("Received login request for ID: " + teacher.getId()); // 添加日志
            Teacher existingTeacher = teacherMapper.findById(teacher.getId());
            if (existingTeacher == null) {
                System.out.println("Teacher not found"); // 添加日志
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
            }
            System.out.println("Found teacher: " + existingTeacher.getId()); // 添加日志
            if (!existingTeacher.getPw().equals(teacher.getPw())) {
                System.out.println("Password mismatch"); // 添加日志
                System.out.println("Expected: " + existingTeacher.getPw()); // 添加日志
                System.out.println("Received: " + teacher.getPw()); // 添加日志
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
            }
            System.out.println("Login successful"); // 添加日志
            return ResponseEntity.ok("登录成功");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}
